<?php
namespace library;

use idea\ap\Ap;
use idea\ap\Registry;
use idea\ap\Singleton;
use idea\ap\UserIdentity;
use idea\mvc\Mvc;
use idea\saf\Conf;
use helper\ErrorNo;
use helper\Util;
use helper\Log;

/**
 * PageAction abstract class file
 * PageAction基类
 * @author 宋欢 <iphper@yeah.net>
 * @version $Id: PageAction.php 1 2013-04-05 01:08:06Z huan.song $
 * @package library
 * @since 1.0
 */
abstract class PageAction extends BaseAction
{
	/**
	 * @var string 页面首次渲染的布局名
	 */
	public $layoutName = 'main';
	
	/**
	 * 展示页面，输出数据
	 * @param array $data
	 * @param string $tplName
	 * @return void
	 */
	public function render(array $data = array(), $tplName = null)
	{
		$this->assignSystem();
		$this->assignOptions();
		$this->assignUrl();
		$this->assignUser();
		
		$view = Mvc::getView();
		$view->addLayoutName('layouts' . DS . $this->layoutName);
		if ($tplName === null) {
			$tplName = $this->getDefaultTplName();
		}

		if (!isset($data['err_no']) || $data['err_no'] === ErrorNo::SUCCESS_NUM) {
			$data['err_no'] = Ap::getRequest()->getInteger('err_no', ErrorNo::SUCCESS_NUM);
			$data['err_msg'] = Ap::getRequest()->getString('err_msg');
		}
		
		$view->render($tplName, $data);
	}

	/**
	 * 将配置参数和常用数据设置到模板变量中
	 * @return void
	 */
	public function assignSystem()
	{
		$view = Mvc::getView();
		$view->viewDirectory = DIR_APP_VIEWS;
		
		$config = Conf::getAppConf('view');
		isset($config['skin_name']) || $view->skinName = $config['skin_name'];
		isset($config['charset']) || $view->charset = $config['charset'];
		isset($config['tpl_extension']) || $view->tplExtension = $config['tpl_extension'];
		isset($config['version']) || $view->version = $config['version'];
		
		$view->assign('app', 		APP_NAME);
		$view->assign('module', 	Mvc::$module);
		$view->assign('controller', Mvc::$controller);
		$view->assign('action', 	Mvc::$action);
		$view->assign('log_id', 	Log::getId());
		$view->assign('util', 		Singleton::getInstance('helper\Util'));
		if (($wfBackTrace = Registry::get('warning_backtrace')) !== null) {
			$view->assign('warning_backtrace', $wfBackTrace);
		}
	}

	/**
	 * 将站点配置设置到模板变量中
	 * @return void
	 */
	public function assignOptions()
	{
		$view = Mvc::getView();
		$ret = Util::getService('Options', 'system')->findAll();
		if ($ret['err_no'] === ErrorNo::SUCCESS_NUM) {
			$view->assign('options', $ret['data']);
		}
	}

	/**
	 * 将链接信息设置到模板变量中
	 * @return void
	 */
	public function assignUrl()
	{
		$view = Mvc::getView();

		$baseUrl       = Ap::getRequest()->getBaseUrl();
		$basePath      = Ap::getRequest()->getBasePath();
		$scriptUrl     = Ap::getRequest()->getScriptUrl();

		$staticUrl = $baseUrl . '/static/' . APP_NAME . '/' . $view->skinName;
		$logoutUrl = Util::getUrl('logout', 'show', 'admin');

		$view->assign('root_url',       $baseUrl . '/..');
		$view->assign('base_path',      $basePath);
		$view->assign('base_url',       $baseUrl);
		$view->assign('script_url',     $scriptUrl);
		$view->assign('static_url',     $staticUrl);
		$view->assign('logout_url',     $logoutUrl);
		$view->assign('js_url',         $staticUrl . '/js');
		$view->assign('css_url',        $staticUrl . '/css');
		$view->assign('imgs_url',       $staticUrl . '/imgs');
	}
	
	/**
	 * 将用户信息设置到模板变量中
	 * @return void
	 */
	public function assignUser()
	{
		$view = Mvc::getView();
		$view->assign('user_id', UserIdentity::getId());
	}
	
	/**
	 * 获取默认的模版名
	 * @return string
	 */
	public function getDefaultTplName()
	{
		return Mvc::$module . DS . Mvc::$controller . '_' . Mvc::$action;
	}
}
